package demo1;

import javax.lang.model.element.NestingKind;
import java.util.Locale;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 861833
 */
class Student{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
public class Test {
    public static void main(String[] args) {
        //capacity
        StringBuilder stringBuilder = new StringBuilder("hello");
        System.out.println("初始容量: " + stringBuilder.capacity());
        stringBuilder.ensureCapacity(50);
        System.out.println("扩容后容量: " +stringBuilder.capacity());
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("hello");
        System.out.println("初始容量: " + stringBuffer.capacity());
        stringBuffer.ensureCapacity(50);
        System.out.println("扩容后容量: " +stringBuffer.capacity());
    }
    public static void main30(String[] args) {
        //capacity
        StringBuilder stringBuilder = new StringBuilder("hello");
        System.out.println(stringBuilder.capacity());//返回值 21
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("hello");
        System.out.println(stringBuffer.capacity());//返回值 21
    }
    public static void main29(String[] args) {
        //reverse
        StringBuilder stringBuilder = new StringBuilder("hello");
        System.out.println(stringBuilder.toString());//返回值 hello
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("hello");
        System.out.println(stringBuffer.toString());//返回值 hello
    }
    public static void main28(String[] args) {
        //reverse
        StringBuilder stringBuilder = new StringBuilder("hello");
        System.out.println(stringBuilder.reverse());//返回值 olleh
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("hello");
        System.out.println(stringBuffer.reverse());//返回值 olleh
    }
    public static void main27(String[] args) {
        //substring
        StringBuilder stringBuilder = new StringBuilder("abcdefg");
        String str1 = stringBuilder.substring(2,5); //返回值 cde
        System.out.println(str1);
        StringBuffer stringBuffer = new StringBuffer("abcdefg");
        String str2 = stringBuffer.substring(2,5);//返回值 cde
        System.out.println(str2);
        /*StringBuilder stringBuilder = new StringBuilder("abcdefg");
        String str1 = stringBuilder.substring(3); //返回值 defg
        System.out.println(str1);
        StringBuffer stringBuffer = new StringBuffer("abcdefg");
        String str2 = stringBuffer.substring(3);//返回值 defg
        System.out.println(str2);*/
    }
    public static void main26(String[] args) {
        //replace
        StringBuilder stringBuilder = new StringBuilder("abcdefg");
        stringBuilder.replace(1,5,"hhhh"); //返回值 ahhhhfg
        System.out.println(stringBuilder);
        StringBuffer stringBuffer = new StringBuffer("abcdefg");
        stringBuffer.replace(1,5,"hhhh");//返回值 ahhhhfg
        System.out.println(stringBuffer);

    }
    public static void main25(String[] args) {
        //lastIndexOf
        /*StringBuilder stringBuilder = new StringBuilder("ababcabcdabcde");
        System.out.println(stringBuilder.lastIndexOf("abc"));//返回值 9
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("ababcabcdabcde");
        System.out.println(stringBuilder.lastIndexOf("abc"));//返回值 9*/
        StringBuilder stringBuilder = new StringBuilder("ababcabcdabcde");
        System.out.println(stringBuilder.lastIndexOf("abc",8));//返回值 5
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("ababcabcdabcde");
        System.out.println(stringBuilder.lastIndexOf("abc",4));//返回值 2


    }
    public static void main24(String[] args) {
        //indexOf
        StringBuilder stringBuilder = new StringBuilder("ababcabcdabcde");
        System.out.println(stringBuilder.indexOf("abc",3));//返回值 5
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("ababcabcdabcde");
        System.out.println(stringBuilder.indexOf("abc",3));//返回值 5
        /*StringBuilder stringBuilder = new StringBuilder("ababcabcdabcde");
        System.out.println(stringBuilder.indexOf("abc"));//返回值 2
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("ababcabcdabcde");
        System.out.println(stringBuilder.indexOf("abc"));//返回值 2*/
    }
    public static void main23(String[] args) {
        //length
        StringBuilder stringBuilder = new StringBuilder("hello");
        System.out.println(stringBuilder.length());//返回值 5
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("hello");
        System.out.println(stringBuffer.length());//返回值 5
    }
    public static void main22(String[] args) {
        //delete
        StringBuilder stringBuilder = new StringBuilder("hello");
        stringBuilder.delete(2,4);
        System.out.println(stringBuilder);//返回值 heo
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("hello");
        stringBuffer.delete(1,4);
        System.out.println(stringBuffer);//返回值 ho
    }
    public static void main21(String[] args) {
        //deleteCharAt
        StringBuilder stringBuilder = new StringBuilder("hello");
        stringBuilder.deleteCharAt(2);
        System.out.println(stringBuilder);//返回值 helo
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("hello");
        stringBuffer.deleteCharAt(1);
        System.out.println(stringBuffer);//返回值 hllo
    }
    public static void main20(String[] args) {
        //setCharAt
        StringBuilder stringBuilder = new StringBuilder("hello");
        stringBuilder.setCharAt(2,'a');
        System.out.println(stringBuilder);//返回值 healo
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("hello");
        stringBuffer.setCharAt(3,'b');
        System.out.println(stringBuffer);//返回值 helbo
    }
    public static void main19(String[] args) {
        //charAt
        StringBuilder stringBuilder = new StringBuilder("hello");
        System.out.println(stringBuilder.charAt(2));
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("hello");
        System.out.println(stringBuffer.charAt(1));



    }
    public static void main18(String[] args) {
        //append
        StringBuilder stringBuilder = new StringBuilder("hello");
        stringBuilder.append("world");
        System.out.println(stringBuilder);
        System.out.println("============");
        StringBuffer stringBuffer = new StringBuffer("hello");
        stringBuffer.append("world");
        System.out.println(stringBuffer);

    }
    public static void main17(String[] args) {

        String str = " hello world hello world";
        String str1 = str.trim();
        System.out.println(str1);
        /* final char[] ch = new char[]{'h','e','l','l','o'};
        ch[0] = 'c'; //修改字符数组中的内容
        ch = new char[10]; //字符数组不能引用另外一个数组*/
    }
    public static void main16(String[] args) {
        //截取
        String str = "helloworld";
        String str2 = str.substring(5,7);
        System.out.println(str2); //结果为 wo
        String str1 = str.substring(4);
        System.out.println(str1);

    }
    public static void main15(String[] args) {
        //拆分
        String str3 = "name=小贺&&age=20";
        String[] strings4 = str3.split("=|&&");
        for (int i = 0; i < strings4.length; i++) {
            System.out.println(strings4[i]);
        }

        String str2 = "192\\168\\0\\0";
        String[] strings3 =str2.split("\\\\");
        for (int i = 0; i < strings3.length; i++) {
            System.out.println(strings3[i]);
        }
        String str1 = "192.168.0.0";
        String[] strings2 =str1.split("\\.");
        for (int i = 0; i < strings2.length; i++) {
            System.out.println(strings2[i]);
        }
        String str = "hello world hello world hello world hello world";
        String[] strings1 = str.split(" ",3);
        for (int i = 0; i < strings1.length; i++) {
            System.out.println(strings1[i]);
        }
        String[] strings = str.split(" ");
        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
        }

    }
    public static void main14(String[] args) {
        //替换
        String str = "helloworld";
        String str2 = str.replaceFirst("l","-");
        System.out.println(str2);

        /*String str1 = str.replaceAll("l","-");
        System.out.println(str1);*/
    }
    public static void main13(String[] args) {
        //格式化
        String str = String.format("%d-%d-%d",2025,5,26);
        System.out.println(str);
        String str1 = String.format("%d/%d/%d",2025,5,26);
        System.out.println(str1);
    }
    public static void main12(String[] args) {
        //小写转大写
        String str1 = "helloworld";
        System.out.println(str1.toUpperCase());
        //大写转小写
        String str2 = "HELLOWORLD";
        System.out.println(str2.toLowerCase());
    }
    public static void main11(String[] args) {
        String str = "hello";
        char[] ch = str.toCharArray();
        /*for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i]+" ");
        }*/
        /*String str1 = String.valueOf(ch);
        System.out.println(str1);*/
        String string = new String(ch);
        System.out.println(string);
    }
    public static void main10(String[] args) {
        int data1 = Integer.parseInt("1234");
        Double data2 =Double.parseDouble("123.4");
        System.out.println(data1);
        System.out.println(data2);

    }
    public static void main9(String[] args) {
        String str1 = String.valueOf(123);
        String str2 = String.valueOf('a');
        String str3 = String.valueOf(10l);
        String str4 = String.valueOf(3.14f);
        String str5 = String.valueOf(123.4);
        String str6 = String.valueOf(true);
        String str7 = String.valueOf(new Student("小贺",20));
        char[] ch = new char[]{'h','e','l','l','o'};
        String str8 = String.valueOf(ch);
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println(str4);
        System.out.println(str5);
        System.out.println(str6);
        System.out.println(str7);
        System.out.println(str8);


    }
    public static void main8(String[] args) {
        String str = "ababcabcdabcdeabcdef";
        int i = str.lastIndexOf('a');
        int i1 = str.lastIndexOf('a',12);
        int i2 = str.lastIndexOf("abc");
        int i3 = str.lastIndexOf("abc",8);
        System.out.println(i);
        System.out.println(i1);
        System.out.println(i2);
        System.out.println(i3);
    }
    public static void main7(String[] args) {
        String str = "ababcabcdabcdeabcdef";
        int i = str.indexOf('a');
        int i1 = str.indexOf('a',3);
        int i2 = str.indexOf("abc");
        int i3 = str.indexOf("abc",3);
        System.out.println(i);
        System.out.println(i1);
        System.out.println(i2);
        System.out.println(i3);
        /*System.out.println(str.length());
        char ch = str.charAt(21);
        System.out.println(ch); //返回值 a*/
    }
    public static void main6(String[] args) {
        String str1 = new String("ab");
        String str2 = new String("abc");
        String str3 = new String("cdef");
        String str4 = new String("AB");
        System.out.println(str1.compareToIgnoreCase(str2));
        System.out.println(str1.compareToIgnoreCase(str3));
        System.out.println(str1.compareToIgnoreCase(str4));
        System.out.println(str2.compareToIgnoreCase(str1));
    }
    public static void main5(String[] args) {
        String str1 = new String("ab");
        String str2 = new String("ac");
        String str3 = new String("cdf");
        System.out.println(str1.compareTo(str2)); // 返回值为 -1
        System.out.println(str1.compareTo(str3)); //返回值为 -2 （a-c = -2）
    }
    public static void main4(String[] args) {
        String str1 = new String("ab");
        String str2 = new String("abc");
        String str3 = new String("abcdef");
        String str4 = new String("ab");
        System.out.println(str1.compareTo(str2));
        System.out.println(str1.compareTo(str3));
        System.out.println(str1.compareTo(str4));
        System.out.println(str2.compareTo(str3));
        System.out.println(str2.compareTo(str1));
    }
    public static void main3(String[] args) {
        String str1 = new String("hello");
        String str2 = new String("world");
        String str3 = new String("hello");
        System.out.println(str1 == str2);
        System.out.println(str1 == str3);
        System.out.println(str2 == str3);
        System.out.println("调用String类的equals方法");
        System.out.println(str1.equals(str2));
        System.out.println(str1.equals(str3));
        System.out.println(str2.equals(str3));

    }
    public static void main2(String[] args) {
        int a = 10;
        int b = 20;
        int c = 10;
        System.out.println(a == b);
        System.out.println(a == c);
        System.out.println(b == c);
        System.out.println("========");
        String str1 = new String("hello");
        String str2 = new String("world");
        String str3 = new String("hello");
        String str4 = str1;
        System.out.println(str1 == str2);
        System.out.println(str1 == str3);
        System.out.println(str1 == str4);
        System.out.println(str3 == str4);
        /*int len = "helloworld".length()*/;
    }
    public static void main1(String[] args) {

        System.out.println("===常量串===");
        String str = "Hello World!";
        System.out.println(str);
        System.out.println("===new String对象===");
        String string = new String("Hello World!");
        System.out.println(string);
        System.out.println("===字符数组===");
        char[] arr = {'H','e','l','l','o','W','o','r','l','d','!'};
        String str2 = new String(arr);
        System.out.println(str2);
    }
}
